////对有序数组的二分查找
//public class java0522 {
//    public static void main(String[] args) {
//
//        int[] array = {1, 2, 3, 4, 5};
//        int num = find(array);
//        System.out.println(num);
//
//    }
//
//    public static int find(int[] array){
//
//                int left = 0;
//                int right = array.length - 1;
//                int mid = (left + right) / 2;
//                int key = 3;
//                while(left <= right){
//                    if(key < array[mid]){
//                        right = mid - 1;
//                    }else if(key > array[mid]){
//                        left = mid + 1;
//                    }else{
//                      return array[mid];
//                    }
//                }
//                return -1;
//            }
//
//
//}


import java.util.Arrays;

//冒泡排序
public class java0522 {
    public static void main(String[] args) {
        int[] array = {1, 8, 9, 34, 2, 7};
        bubblesort(array);
        System.out.println(Arrays.toString(array));
        int num = find(array);

        System.out.println(num);
    }
    public static void bubblesort(int[] array){
        int i = 0;
        int j = 0;
        for(i = 0;i < array.length-1;i++){
            boolean flag = false;
            for(j = 0;j < array.length-1-i;j++){
                if(array[j] > array[j+1]){
                    int temp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = temp;
                    flag = true;
                }
            }

            if(!flag){
                break;
            }

        }

    }

    public static int find(int[] array){

        int left = 0;
        int right = array.length - 1;

        int key = 34;
        while(left <= right){
            int mid = (left + right) / 2;  //对于变的量一定要放循环里
            if(key < array[mid]){
                right = mid - 1;
            }else if(key > array[mid]){
                left = mid + 1;
            }else{
                return array[mid];
            }
        }
        return -1;
    }

}










































